import ddddocr


class VerificationCodeOCR:
    """通过第三方库ddddocr对验证码进行识别（仅支持3.9及以下版本的Python）"""

    def __init__(self):
        self.ocr = ddddocr.DdddOcr(show_ad=False)

    def get_verification_code(self, file):
        """
        :param file: 待识别的图片路径，或对应的bytes
        """
        print(f'--type--{type(file)}')
        img_bytes = file
        if type(file) == str:
            with open(file, 'rb') as f:
                img_bytes = f.read()
        verification_code = self.ocr.classification(img_bytes)
        return verification_code


if __name__ == '__main__':
    ocr = VerificationCodeOCR()
    # text = ocr.get_verification_code(r'C:\Users\戴茂\Pictures\Snipaste\20230504\152253.png')
    text = ocr.get_verification_code(r'C:\Users\戴茂\Desktop\9999.png')
    print(text)
